package com.eastfair.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.pay.dto.*;
import com.eastfair.pay.entity.InvoiceApplyer;
import com.eastfair.pay.service.InvoiceApplyerService;
import com.eastfair.pay.vo.InvoiceApplyerVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 发票申请
 * </p>
 *
 * @author clm
 * @date 2022-08-06
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/invoiceApplyer")
@Api(value = "InvoiceApplyer", tags = "发票申请")
@PreAuth(replace = "pay:invoiceApplyer:")
public class InvoiceApplyerController extends SuperController<InvoiceApplyerService, Long, InvoiceApplyer, InvoiceApplyerPageQuery, InvoiceApplyerSaveDTO, InvoiceApplyerUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Autowired
    private InvoiceApplyerService invoiceApplyerService;

    @Override
    public void handlerResult(IPage<InvoiceApplyer> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<InvoiceApplyer> invoiceApplyerList = list.stream().map((map) -> {
            InvoiceApplyer invoiceApplyer = InvoiceApplyer.builder().build();
            //TODO 请在这里完成转换
            return invoiceApplyer;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(invoiceApplyerList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<InvoiceApplyerPageQuery> params) {
        InvoiceApplyerPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }


    /**
     * @return com.eastfair.core.base.R
     * @Author clm
     * @Description //保存发票申请
     * @Date 17:13 2023/2/7
     * @Param [invoiceApplyerDTO]
     **/
    @PostMapping("/save")
    @SysLog("保存发票申请")
    public R saveInvoiceApplyer(@RequestBody @Validated InvoiceApplyerDTO invoiceApplyerDTO) {
        invoiceApplyerService.saveInvoiceApplyer(invoiceApplyerDTO);
        return R.success();
    }

    /**
     * @return com.eastfair.core.base.R
     * @Author clm
     * @Description //更新发票申请
     * @Date 17:13 2023/2/7
     * @Param [invoiceApplyerDTO]
     **/
    @PostMapping("/update")
    @SysLog("更新发票申请")
    public R updateInvoiceApplyer(@RequestBody @Validated InvoiceApplyerDTO invoiceApplyerDTO) {
        invoiceApplyerService.updateInvoiceApplyer(invoiceApplyerDTO);
        return R.success();
    }


    /**
     * @return com.eastfair.core.base.R
     * @Author clm
     * @Description //开票成功标识  假的  等支付系统对接
     * @Date 17:17 2023/2/7
     * @Param [applyId]
     **/
    @GetMapping("/invoice/{applyId}")
    public R invoice(@PathVariable("applyId") Long applyId) {
        invoiceApplyerService.invoice(applyId);
        return R.success();
    }

    /**
     * @return com.eastfair.core.base.R<java.lang.Boolean>
     * @Author fwl
     * @Description //荆艺开票审核
     * @Date 11:32 2023/3/3
     * @Param [invoiceDTO]
     **/
    @PostMapping("/financialExamine")
    public R financialExamine(@RequestBody JYInvoiceDTO invoiceDTO) {
        try {
            baseService.financialExamine(invoiceDTO);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz);
        }

    }


    /**
     * 发票申请查询
     *
     * @return
     */
    @PostMapping("/queryList")
    public R<Page<InvoiceApplyerVO>> queryList(@RequestBody PageParams<InvoiceApplyerPageQuery> params) {
        try {
            return R.success(baseService.queryList(params));
        } catch (BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 发票申请查询
     *
     * @return
     */
    @GetMapping("/queryInfoById/{invoiceApplyerId}")
    public R<InvoiceApplyerVO> queryInfoById(@PathVariable("invoiceApplyerId") Long invoiceApplyerId) {
        try {
            return R.success(baseService.queryInfoById(invoiceApplyerId));
        } catch (BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 发票申请查询
     *
     * @return
     */
    @GetMapping("/queryInfoByBuinessId/{buinessId}")
    public R<InvoiceApplyerVO> queryInfoByBuinessId(@PathVariable("buinessId") Long buinessId) {
        try {
            return R.success(baseService.queryInfoByBuinessId(buinessId));
        } catch (BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 通过业务主键集合查询发票申请状态集合
     *
     * @param buinessIds
     * @return com.eastfair.core.base.R<java.util.List < com.eastfair.pay.vo.InvoiceApplyerVO>>
     * @author duxiaobo
     * @date 2023/4/12 11:20
     */
    @PostMapping("/queryExamineState/buinessIds")
    public R<List<InvoiceApplyerVO>> queryInvoiceExamineState(@RequestBody List<Long> buinessIds) {
        try {
            return R.success(baseService.queryInvoiceExamineState(buinessIds));
        } catch (BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * @return com.eastfair.core.base.R<java.lang.Boolean>
     * @Author clm
     * @Description //审核
     * @Date 11:32 2023/2/14
     * @Param [invoice]
     **/
    @PostMapping("/examine")
    public R<Boolean> examineInvoiceApplyer(@RequestBody InvoiceApplyerDTO invoiceApplyer) {
        try {
            baseService.examineInvoiceApplyer(invoiceApplyer);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 取消开票
     *
     * @param id
     * @return com.eastfair.core.base.R<java.lang.Boolean>
     * @author duxiaobo
     * @date 2023/4/10 11:54
     */
    @GetMapping("/cancel/{id}")
    @SysLog("取消发票申请")
    public R<Boolean> cancelInvoiceApplyer(@PathVariable("id") Long id) {
        try {
            baseService.cancelInvoiceApplyer(id);
            return R.success(true);
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }


    /**
     * 取消开票
     *
     * @param orderNum
     * @return com.eastfair.core.base.R<java.lang.Boolean>
     * @author duxiaobo
     * @date 2023/4/10 11:54
     */
    @GetMapping("/cancelByOrderNum/{orderNum}")
    @SysLog("取消发票申请")
    public R<Boolean> cancelInvoiceApplyer(@PathVariable("orderNum") String orderNum) {
        try {
            baseService.cancelInvoiceApplyerByOrderNum(orderNum);
            return R.success(true);
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }
}
